home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / cprog / f2c3.2src.lha / f2c-for-SASC651 / src / niceprintf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-30  |  10.5 KB  |  442 lines

  1. /****************************************************************
  2. Copyright 1990, 1991, 1993, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "names.h"
  26. #include "output.h"
  27. #ifndef KR_headers
  28. #include "stdarg.h"
  29. #endif
  30.  
  31. #ifdef AMIGA
  32. #undef putc
  33. #endif
  34.  
  35. #define TOO_LONG_INDENT (2 * tab_size)
  36. #define MAX_INDENT 44
  37. #define MIN_INDENT 22
  38. static int last_was_newline = 0;
  39. int indent = 0;
  40. int in_comment = 0;
  41. int in_define = 0;
  42.  extern int gflag1;
  43.  extern char *file_name;
  44.  
  45.  static void ind_printf Argdcl((int, FILE*, char*, va_list));
  46.  
  47.  static void
  48. #ifdef KR_headers
  49. write_indent(fp, use_indent, extra_indent, start, end)
  50.     FILE *fp;
  51.     int use_indent;
  52.     int extra_indent;
  53.     char *start;
  54.     char *end;
  55. #else
  56. write_indent(FILE *fp, int use_indent, int extra_indent, char *start, char *end)
  57. #endif
  58. {
  59.     int ind, tab;
  60.  
  61.     if (gflag1 && last_was_newline)
  62.     fprintf(fp, "#line %ld \"%s\"\n", lineno, infname ? infname : file_name);
  63.     if (in_define == 1) {
  64.     in_define = 2;
  65.     use_indent = 0;
  66.     }
  67.     if (last_was_newline && use_indent) {
  68.     if (*start == '\n') do {
  69.         putc('\n', fp);
  70.         if (++start > end)
  71.             return;
  72.         }
  73.         while(*start == '\n');
  74.  
  75.     ind = indent <= MAX_INDENT
  76.         ? indent
  77.         : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  78.  
  79.     tab = ind + extra_indent;
  80.  
  81.     while (tab > 7) {
  82.         putc ('\t', fp);
  83.         tab -= 8;
  84.     } /* while */
  85.  
  86.     while (tab-- > 0)
  87.         putc (' ', fp);
  88.     } /* if last_was_newline */
  89.  
  90.     while (start <= end)
  91.     putc (*start++, fp);
  92. } /* write_indent */
  93.  
  94. #ifdef KR_headers
  95. /*VARARGS2*/
  96.   void
  97.  margin_printf (fp, a, b, c, d, e, f, g)
  98.   FILE *fp;
  99.   char *a;
  100.   long b, c, d, e, f, g;
  101. {
  102.     ind_printf (0, fp, a, b, c, d, e, f, g);
  103. } /* margin_printf */
  104.  
  105. /*VARARGS2*/
  106.   void
  107.  nice_printf (fp, a, b, c, d, e, f, g)
  108.   FILE *fp;
  109.   char *a;
  110.   long b, c, d, e, f, g;
  111. {
  112.     ind_printf (1, fp, a, b, c, d, e, f, g);
  113. } /* nice_printf */
  114. #define SPRINTF(x,a,b,c,d,e,f,g) sprintf(x,a,b,c,d,e,f,g)
  115.  
  116. #else /* if (!defined(KR_HEADERS)) */
  117.  
  118. #define SPRINTF(x,a,b,c,d,e,f,g) vsprintf(x,a,ap)
  119.  
  120.   void
  121.  margin_printf(FILE *fp, char *fmt, ...)
  122. {
  123.     va_list ap;
  124.     va_start(ap,fmt);
  125.     ind_printf(0, fp, fmt, ap);
  126.     va_end(ap);
  127.     }
  128.  
  129.   void
  130.  nice_printf(FILE *fp, char *fmt, ...)
  131. {
  132.     va_list ap;
  133.     va_start(ap,fmt);
  134.     ind_printf(1, fp, fmt, ap);
  135.     va_end(ap);
  136.     }
  137. #endif
  138.  
  139. #define  max_line_len c_output_line_length
  140.          /* 74Number of characters allowed on an output
  141.                        line.  This assumes newlines are handled
  142.                        nicely, i.e. a newline after a full text
  143.                        line on a terminal is ignored */
  144.  
  145. /* output_buf   holds the text of the next line to be printed.  It gets
  146.    flushed when a newline is printed.   next_slot   points to the next
  147.    available location in the output buffer, i.e. where the next call to
  148.    nice_printf will have its output stored */
  149.  
  150. static char *output_buf;
  151. static char *next_slot;
  152. static char *string_start;
  153.  
  154. static char *word_start = NULL;
  155. static int cursor_pos = 0;
  156. static int In_string = 0;
  157.  
  158.  void
  159. np_init(Void)
  160. {
  161.     next_slot = output_buf = Alloc(MAX_OUTPUT_SIZE);
  162.     memset(output_buf, 0, MAX_OUTPUT_SIZE);
  163.     }
  164.  
  165.  static char *
  166. #ifdef KR_headers
  167. adjust_pointer_in_string(pointer)
  168.     register char *pointer;
  169. #else
  170. adjust_pointer_in_string(register char *pointer)
  171. #endif
  172. {
  173.     register char *s, *s1, *se, *s0;
  174.  
  175.     /* arrange not to break \002 */
  176.     s1 = string_start ? string_start : output_buf;
  177.     for(s = s1; s < pointer; s++) {
  178.         s0 = s1;
  179.         s1 = s;
  180.         if (*s == '\\') {
  181.             se = s++ + 4;
  182.             if (se > pointer)
  183.                 break;
  184.             if (*s < '0' || *s > '7')
  185.                 continue;
  186.             while(++s < se)
  187.                 if (*s < '0' || *s > '7')
  188.                     break;
  189.             --s;
  190.             }
  191.         }
  192.     return s0 - 1;
  193.     }
  194.  
  195. /* ANSI says strcpy's behavior is undefined for overlapping args,
  196.  * so we roll our own fwd_strcpy: */
  197.  
  198.  static void
  199. #ifdef KR_headers
  200. fwd_strcpy(t, s)
  201.     register char *t;
  202.     register char *s;
  203. #else
  204. fwd_strcpy(register char *t, register char *s)
  205. #endif
  206. { while(*t++ = *s++); }
  207.  
  208. /* isident -- true iff character could belong to a unit.  C allows
  209.    letters, numbers and underscores in identifiers.  This also doubles as
  210.    a check for numeric constants, since we include the decimal point and
  211.    minus sign.  The minus has to be here, since the constant "10e-2"
  212.    cannot be broken up.  The '.' also prevents structure references from
  213.    being broken, which is a quite acceptable side effect */
  214.  
  215. #define isident(x) (Tr[x] & 1)
  216. #define isntident(x) (!Tr[x])
  217.  
  218.   static void
  219. #ifdef KR_headers
  220.  ind_printf (use_indent, fp, a, b, c, d, e, f, g)
  221.   int use_indent;
  222.   FILE *fp;
  223.   char *a;
  224.   long b, c, d, e, f, g;
  225. #else
  226.  ind_printf (int use_indent, FILE *fp, char *a, va_list ap)
  227. #endif
  228. {
  229.     extern int max_line_len;
  230.     extern FILEP c_file;
  231.     extern char tr_tab[];    /* in output.c */
  232.     register char *Tr = tr_tab;
  233.     int ch, inc, ind;
  234.     static int extra_indent, last_indent, set_cursor = 1;
  235.  
  236.     cursor_pos += indent - last_indent;
  237.     last_indent = indent;
  238.     SPRINTF (next_slot, a, b, c, d, e, f, g);
  239.  
  240.     if (fp != c_file) {
  241.     fprintf (fp,"%s", next_slot);
  242.     return;
  243.     } /* if fp != c_file */
  244.  
  245.     do {
  246.     char *pointer;
  247.  
  248. /* The   for   loop will parse one output line */
  249.  
  250.     if (set_cursor) {
  251.         ind = indent <= MAX_INDENT
  252.             ? indent
  253.             : MIN_INDENT + indent % (MAX_INDENT - MIN_INDENT);
  254.         cursor_pos = ind + extra_indent;
  255.         set_cursor = 0;
  256.         }
  257.     if (in_comment)
  258.             for (pointer = next_slot; *pointer && *pointer != '\n' &&
  259.                 cursor_pos <= max_line_len; pointer++)
  260.             cursor_pos++;
  261.     else
  262.           for (pointer = next_slot; *pointer && *pointer != '\n' &&
  263.         cursor_pos <= max_line_len; pointer++) {
  264.  
  265.         /* Update state variables here */
  266.  
  267.         if (In_string) {
  268.         switch(*pointer) {
  269.             case '\\':
  270.                 if (++cursor_pos > max_line_len) {
  271.                     cursor_pos -= 2;
  272.                     --pointer;
  273.                     goto overflow;
  274.                     }
  275.                 ++pointer;
  276.                 break;
  277.             case '"':
  278.                 In_string = 0;
  279.                 word_start = 0;
  280.             }
  281.         }
  282.         else switch (*pointer) {
  283.             case '"':
  284.             if (cursor_pos + 5 > max_line_len) {
  285.                 word_start = 0;
  286.                 --pointer;
  287.                 goto overflow;
  288.                 }
  289.             In_string = 1;
  290.             string_start = word_start = pointer;
  291.                 break;
  292.             case '\'':
  293.             if (pointer[1] == '\\')
  294.                 if ((ch = pointer[2]) >= '0' && ch <= '7')
  295.                     for(inc = 3; pointer[inc] != '\''
  296.                         && ++inc < 5;);
  297.                 else
  298.                     inc = 3;
  299.             else
  300.                 inc = 2;
  301.             /*debug*/ if (pointer[inc] != '\'')
  302.             /*debug*/  fatalstr("Bad character constant %.10s",
  303.                     pointer);
  304.             if ((cursor_pos += inc) > max_line_len) {
  305.                 cursor_pos -= inc;
  306.                 word_start = 0;
  307.                 --pointer;
  308.                 goto overflow;
  309.                 }
  310.             word_start = pointer;
  311.             pointer += inc;
  312.             break;
  313.         case '\t':
  314.             cursor_pos = 8 * ((cursor_pos + 8) / 8) - 1;
  315.             break;
  316.         default: {
  317.  
  318. /* HACK  Assumes that all characters in an atomic C token will be written
  319.    at the same time.  Must check for tokens first, since '-' is considered
  320.    part of an identifier; checking isident first would mean breaking up "->" */
  321.  
  322.             if (word_start) {
  323.             if (isntident(*(unsigned char *)pointer))
  324.                 word_start = NULL;
  325.             }
  326.             else if (isident(*(unsigned char *)pointer))
  327.             word_start = pointer;
  328.             break;
  329.         } /* default */
  330.         } /* switch */
  331.         cursor_pos++;
  332.     } /* for pointer = next_slot */
  333.  overflow:
  334.     if (*pointer == '\0') {
  335.  
  336. /* The output line is not complete, so break out and don't output
  337.    anything.  The current line fragment will be stored in the buffer */
  338.  
  339.         next_slot = pointer;
  340.         break;
  341.     } else {
  342.         char last_char;
  343.         int in_string0 = In_string;
  344.  
  345. /* If the line was too long, move   pointer   back to the character before
  346.    the current word.  This allows line breaking on word boundaries.  Make
  347.    sure that 80 character comment lines get broken up somehow.  We assume
  348.    that any non-string 80 character identifier must be in a comment.
  349. */
  350.  
  351.         if (*pointer == '\n')
  352.         in_define = 0;
  353.         else if (word_start && word_start > output_buf)
  354.         if (In_string)
  355.             if (string_start && pointer - string_start < 5)
  356.                 pointer = string_start - 1;
  357.             else {
  358.                 pointer = adjust_pointer_in_string(pointer);
  359.                 string_start = 0;
  360.                 }
  361.         else if (word_start == string_start
  362.                 && pointer - string_start >= 5) {
  363.             pointer = adjust_pointer_in_string(next_slot);
  364.             In_string = 1;
  365.             string_start = 0;
  366.             }
  367.         else
  368.             pointer = word_start - 1;
  369.         else if (cursor_pos > max_line_len) {
  370. #ifndef ANSI_Libraries
  371.         extern char *strchr();
  372. #endif
  373.         if (In_string) {
  374.             pointer = adjust_pointer_in_string(pointer);
  375.             if (string_start && pointer > string_start)
  376.                 string_start = 0;
  377.             }
  378.         else if (strchr("&*+-/<=>|", *pointer)
  379.             && strchr("!%&*+-/<=>^|", pointer[-1])) {
  380.             pointer -= 2;
  381.             if (strchr("<>", *pointer)) /* <<=, >>= */
  382.                 pointer--;
  383.             }
  384.         else {
  385.             if (word_start)
  386.                 while(isident(*(unsigned char *)pointer))
  387.                     pointer++;
  388.             pointer--;
  389.             }
  390.         }
  391.         last_char = *pointer;
  392.         write_indent(fp, use_indent, extra_indent, output_buf, pointer);
  393.         next_slot = output_buf;
  394.         if (In_string && !string_start && Ansi == 1 && last_char != '\n')
  395.         *next_slot++ = '"';
  396.         fwd_strcpy(next_slot, pointer + 1);
  397.  
  398. /* insert a line break */
  399.  
  400.         if (last_char == '\n') {
  401.         if (In_string)
  402.             last_was_newline = 0;
  403.         else {
  404.             last_was_newline = 1;
  405.             extra_indent = 0;
  406.             }
  407.         }
  408.         else {
  409.         extra_indent = TOO_LONG_INDENT;
  410.         if (In_string && !string_start) {
  411.             if (Ansi == 1) {
  412.                 fprintf(fp, "\"\n");
  413.                 use_indent = 1;
  414.                 last_was_newline = 1;
  415.                 }
  416.             else {
  417.                 fprintf(fp, "\\\n");
  418.                 last_was_newline = 0;
  419.                 }
  420.             In_string = in_string0;
  421.             }
  422.         else {
  423.             if (in_define)
  424.                 putc('\\', fp);
  425.             putc ('\n', fp);
  426.             last_was_newline = 1;
  427.             }
  428.         } /* if *pointer != '\n' */
  429.  
  430.         if (In_string && Ansi != 1 && !string_start)
  431.         cursor_pos = 0;
  432.         else
  433.         set_cursor = 1;
  434.  
  435.         string_start = word_start = NULL;
  436.  
  437.     } /* else */
  438.  
  439.     } while (*next_slot);
  440.  
  441. } /* ind_printf */
  442.